package zozopo.warehouse.operations;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zozopo.comm.ZOZOPOCODE;
import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.operations.comm.DictionaryOS;
import zozopo.operations.comm.OperatingService;

public class ReceiptrecordOS extends OperatingService{
	
	public ReceiptrecordOS(Connection conn){
		super(conn);
	}
	
	public ReceiptrecordOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<ReceiptrecordTB> selectListBySql(String sql){
		List<ReceiptrecordTB> receiptrecordList = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		
		if(resultSet != null){
				try {
					receiptrecordList = new ArrayList<ReceiptrecordTB>();
					while(resultSet.next()){
						ReceiptrecordTB receiptrecord = new ReceiptrecordTB();
						
						receiptrecord.setGoodsid_receiptrecord(resultSet.getInt(ReceiptrecordTB.GOODSID_RECEIPTRECORD));
						receiptrecord.setGoodsname_receiptrecord(resultSet.getString(ReceiptrecordTB.GOODSNAME_RECEIPTRECORD));
						receiptrecord.setGoodunit_receiptrecord(resultSet.getString(ReceiptrecordTB.GOODUNIT_RECEIPTRECORD));
						
						receiptrecord.setId_receiptrecord(resultSet.getInt(ReceiptrecordTB.ID_RECEIPTRECORD));
						receiptrecord.setNumber_receiptrecord(resultSet.getInt(ReceiptrecordTB.NUMBER_RECEIPTRECORD));
						receiptrecord.setPrice_receiptrecord(resultSet.getDouble(ReceiptrecordTB.PRICE_RECEIPTRECORD));
						receiptrecord.setReceiptid_receiptrecord(resultSet.getInt(ReceiptrecordTB.RECEIPTID_RECEIPTRECORD));
						receiptrecord.setReceiptlistid_receiptrecord(resultSet.getInt(ReceiptrecordTB.RECEIPTLISTID_RECEIPTRECORD));
						receiptrecord.setPurchaseid_receiptrecord(resultSet.getInt(ReceiptrecordTB.PURCHASEID_RECEIPTRECORD));
						receiptrecord.setState_receiptrecord(resultSet.getString(ReceiptrecordTB.STATE_RECEIPTRECORD));
						
						receiptrecord.setUseremail_receiptrecord(resultSet.getString(ReceiptrecordTB.USEREMAIL_RECEIPTRECORD));
						receiptrecord.setUsername_receiptrecord(resultSet.getString(ReceiptrecordTB.USERNAME_RECEIPTRECORD));
						
						receiptrecord.setWarehouseid_receiptrecord(resultSet.getInt(ReceiptrecordTB.WAREHOUSEID_RECEIPTRECORD));
						receiptrecord.setWarehousename_receiptrecord(resultSet.getString(ReceiptrecordTB.WAREHOUSENAME_RECEIPTRECORD));
						
						
						
						receiptrecordList.add(receiptrecord);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return receiptrecordList;
	
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<ReceiptrecordTB> selectAll(){
		String sql = "SELECT * FROM " + ReceiptrecordTB.TABLE_NAME;
		List<ReceiptrecordTB> receiptrecordList =  selectListBySql(sql);
		
		
		
		return receiptrecordList;
	}
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean insertReceiptrecord(ReceiptrecordTB receiptrecord){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> insertMap = new HashMap<String, Object>();
		
		
		insertMap.put(ReceiptrecordTB.GOODSID_RECEIPTRECORD,receiptrecord.getGoodsid_receiptrecord());
		//insertMap.put(SupplersTB.DATE_SUPPLIER, goods.getUnit_goods());
		insertMap.put(ReceiptrecordTB.GOODSNAME_RECEIPTRECORD, receiptrecord.getGoodsname_receiptrecord());

		insertMap.put(ReceiptrecordTB.GOODUNIT_RECEIPTRECORD, receiptrecord.getGoodunit_receiptrecord());
		
	
		insertMap.put(ReceiptrecordTB.WAREHOUSEID_RECEIPTRECORD, receiptrecord.getWarehouseid_receiptrecord());
		insertMap.put(ReceiptrecordTB.WAREHOUSENAME_RECEIPTRECORD, receiptrecord.getWarehousename_receiptrecord());
		
		insertMap.put(ReceiptrecordTB.RECEIPTLISTID_RECEIPTRECORD, receiptrecord.getReceiptlistid_receiptrecord());
		insertMap.put(ReceiptrecordTB.RECEIPTID_RECEIPTRECORD, receiptrecord.getReceiptid_receiptrecord());
		insertMap.put(ReceiptrecordTB.PURCHASEID_RECEIPTRECORD, receiptrecord.getPurchaseid_receiptrecord());
		
		insertMap.put(ReceiptrecordTB.PRICE_RECEIPTRECORD, receiptrecord.getPrice_receiptrecord());
		insertMap.put(ReceiptrecordTB.PROFIT_RECEIPTRECORD, receiptrecord.getProfit_receiptrecord());
		insertMap.put(ReceiptrecordTB.NUMBER_RECEIPTRECORD, receiptrecord.getNumber_receiptrecord());
		
		
		//insertMap.put(PurchaseTB.TOTAL_PURCHASE, purchase.getPrice_purchase()*purchase.getNumber_purchase());
		
	
		
		insertMap.put(ReceiptrecordTB.USEREMAIL_RECEIPTRECORD, receiptrecord.getUseremail_receiptrecord());
		insertMap.put(ReceiptrecordTB.USERNAME_RECEIPTRECORD, receiptrecord.getUsername_receiptrecord());
		
		
			
		
		return sqlMachine.insertRow(ReceiptrecordTB.TABLE_NAME, insertMap);
	}
	
	
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<ReceiptrecordTB> getList(List<ReceiptTB> receiptList){
		
		boolean isFistTime = true;
		String receiptStr = "";
		for(ReceiptTB receipt:receiptList){
			if(isFistTime){
				receiptStr += receipt.getId_receipt();
				isFistTime = false;
			}else{
				receiptStr += ","+receipt.getId_receipt();
			}
			
		}
		
		String sql = "select * from "+ReceiptrecordTB.TABLE_NAME+" where "+ ReceiptrecordTB.RECEIPTID_RECEIPTRECORD 
		+" in ("+receiptStr+")";
		
		
		return selectListBySql(sql);
	}
	

	/**
	 * @version 1.0
	 * @author Shea
	 */
	public boolean upadte(ReceiptrecordTB receiptrecord){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> updateMap = new HashMap<String, Object>();
		
		updateMap.put(ReceiptrecordTB.PROFIT_RECEIPTRECORD, receiptrecord.getProfit_receiptrecord());
		
		return sqlMachine.updateRow(ReceiptrecordTB.TABLE_NAME, updateMap, ReceiptrecordTB.ID_RECEIPTRECORD, receiptrecord.getId_receiptrecord());
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public String computingProfit(){
		
		String opertionInfo = "";
		//ReceiptrecordOS receiptrecordOS = new ReceiptrecordOS(conn);
		
		List<ReceiptrecordTB> receiptrecordList = selectAll();
		ReceiptOS receiptOS = new ReceiptOS(conn);
		PurchaseOS purchaseOS = new PurchaseOS(conn);
		
		Map<Integer,ReceiptTB> receiptMap = receiptOS.getMap();
		//Map<Integer,List<PurchaseTB>> purchaseMap = purchaseOS.getMapList();
		Map<Integer,PurchaseTB> purchaseMap = purchaseOS.getMap();
		
		for(ReceiptrecordTB receiptrecord:receiptrecordList){
			ReceiptTB receipt = receiptMap.get(receiptrecord.getReceiptid_receiptrecord());
			PurchaseTB purchase = purchaseMap.get(receiptrecord.getPurchaseid_receiptrecord());
			
			double profit = (receipt.getPrice_receipt() - purchase.getPrice_purchase()) * receipt.getNumber_receipt();
			//double receiptTotal = receipt.getPrice_receipt() * receipt.getNumber_receipt();
			
		
			receiptrecord.setProfit_receiptrecord(profit);
			
			//receiptrecord.get
			if(upadte(receiptrecord)){
				opertionInfo += "," + receiptrecord.getId_receiptrecord();
			}else{
				opertionInfo += "," + receiptrecord.getId_receiptrecord() + "";
			}
		}
		
		return opertionInfo;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public String computingProfitForReceipt(){
		ReceiptOS receiptOS = new ReceiptOS(conn);
		
		
		String opertionInfo = "";
		String sql = "SELECT SUM(profit_receiptrecord) as profit_receiptrecord,receiptid_receiptrecord FROM receiptrecord_warehouse GROUP BY receiptid_receiptrecord";
		List<ReceiptrecordTB> receiptrecordList = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		
		if(resultSet != null){
				try {
					receiptrecordList = new ArrayList<ReceiptrecordTB>();
					while(resultSet.next()){
						ReceiptrecordTB receiptrecord = new ReceiptrecordTB();
						receiptrecord.setReceiptid_receiptrecord(resultSet.getInt(ReceiptrecordTB.RECEIPTID_RECEIPTRECORD));
						receiptrecord.setProfit_receiptrecord(resultSet.getDouble(ReceiptrecordTB.PROFIT_RECEIPTRECORD));
						receiptrecordList.add(receiptrecord);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		for(ReceiptrecordTB receiptrecord : receiptrecordList){
			
			ReceiptTB receipt = new ReceiptTB();
			receipt.setId_receipt(receiptrecord.getReceiptid_receiptrecord());
			receipt.setProfit_receipt(receiptrecord.getProfit_receiptrecord());
			if(receiptOS.updateForPorfit(receipt)){
				opertionInfo +=","+receiptrecord.getReceiptid_receiptrecord();
			}else{
				opertionInfo +=",<font color='red'>"+receiptrecord.getReceiptid_receiptrecord()+"</font>";
				
			}
		}
		
		return opertionInfo;
	}
	
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public boolean cancel(List<ReceiptrecordTB> receiptrecordlist){
		
		for(ReceiptrecordTB receiptrecord : receiptrecordlist){
			if(!cancel(receiptrecord.getId_receiptrecord())){
				return false;
			}
			
		}

		return true;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public boolean cancel(int id){
		SqlMachine sqlMachine = new SqlMachine(conn);
		
	
		HashMap<String, Object> updateMap = new HashMap<String, Object>();
		
		
		//updateMap.put(ReceiptrecordTB., true);
		
		return sqlMachine.updateRow(ReceiptrecordTB.TABLE_NAME, updateMap, ReceiptrecordTB.ID_RECEIPTRECORD, id);
	}
	
}
